﻿#include "datadesignertree.h"
#include <QMouseEvent>
#include <QMenu>
#include <QAction>
#include <QMessageBox>
#include <QHeaderView>
#include "dataobjectdialog.h"


DataDesignerTree::DataDesignerTree(QWidget *parent) : QTreeWidget(parent)
{
    this->setWindowTitle(tr("DataDesignerTree"));
    this->setObjectName("WidgetDataDesignerTreeObject");

    this->setMinimumWidth(1000);

    QStringList label;
    label.append(tr("DataType"));
    label.append(tr("MinDataCount"));
    label.append(tr("MaxDataCount"));
    label.append(tr("DataDiscribe"));
    label.append(tr("DataOperate"));
    this->setColumnCount(label.count());
    this->setHeaderLabels(label);
    for(int i=0; i<label.count(); ++i)
    {
        if(i==0 || i== 3 || i==4)
        {
            this->setColumnWidth(i,200);
        }
        else {
            this->setColumnWidth(i,100);
        }

    }
//    this->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
}

void DataDesignerTree::slot_AddData()
{
    DataObjectDialog dlg(this);
    if(dlg.exec() == QDialog::Accepted)
    {
        QTreeWidgetItem* pTreeItem = new QTreeWidgetItem(this);
        dlg.updateDateItem(pTreeItem);
    }
}

void DataDesignerTree::slot_RemoveData()
{
    QTreeWidgetItem* pcurTreeItem = this->currentItem();
    if(nullptr == pcurTreeItem)return;

    if(pcurTreeItem->childCount() > 0)
    {
        if(QMessageBox::warning(this,tr("Warning"),tr("Makesure you want Delete this DataObject and its children DataObject?")) == QMessageBox::Ok)
        {
            if(pcurTreeItem->parent() == nullptr)
            {
                for(int i=0; i<this->topLevelItemCount(); ++i)
                {
                    if(this->topLevelItem(i) == pcurTreeItem)
                    {
                        this->takeTopLevelItem(i);
                        delete pcurTreeItem;
                        pcurTreeItem = nullptr;
                        break;
                    }
                }
            }
            else {
                for(int i=0; i<pcurTreeItem->parent()->childCount(); ++i)
                {
                    if(pcurTreeItem->parent()->child(i) == pcurTreeItem)
                    {
                        pcurTreeItem->parent()->takeChild(i);
                        delete pcurTreeItem;
                        pcurTreeItem = nullptr;
                        break;
                    }
                }
            }
        }
    }
    else {
        if(QMessageBox::warning(this,tr("Warning"),tr("Makesure you want Delete this DataObject?")) == QMessageBox::Ok)
        {
            if(pcurTreeItem->parent() == nullptr)
            {
                for(int i=0; i<this->topLevelItemCount(); ++i)
                {
                    if(this->topLevelItem(i) == pcurTreeItem)
                    {
                        this->takeTopLevelItem(i);
                        delete pcurTreeItem;
                        pcurTreeItem = nullptr;
                        break;
                    }
                }
            }
            else {
                for(int i=0; i<pcurTreeItem->parent()->childCount(); ++i)
                {
                    if(pcurTreeItem->parent()->child(i) == pcurTreeItem)
                    {
                        pcurTreeItem->parent()->takeChild(i);
                        delete pcurTreeItem;
                        pcurTreeItem = nullptr;
                        break;
                    }
                }
            }
        }
    }
}

void DataDesignerTree::slot_AddChildData()
{
    QTreeWidgetItem* pcurTreeItem = this->currentItem();
    if(nullptr == pcurTreeItem)return;

    DataObjectDialog dlg(this);
    if(dlg.exec() == QDialog::Accepted)
    {
        QTreeWidgetItem* pTreeItem = new QTreeWidgetItem(pcurTreeItem);
        dlg.updateDateItem(pTreeItem);
    }
}

void DataDesignerTree::slot_EditData()
{
    QTreeWidgetItem* pcurItem = this->currentItem();
    if(nullptr == pcurItem)return;
    DataObjectDialog dlg(this);
    dlg.updateDialog(pcurItem);
    if(dlg.exec() == QDialog::Accepted)
    {
        dlg.updateDateItem(pcurItem);
    }
}

void DataDesignerTree::mousePressEvent(QMouseEvent *event)
{
    QTreeWidgetItem* pcurItem = this->itemAt(event->pos());
    if(nullptr != pcurItem)
        this->setCurrentItem(pcurItem);

    if(event->button() == Qt::RightButton)
    {
        QMenu menu;
        menu.addAction(QIcon(),tr("AddData"),this,SLOT(slot_AddData()));

        if(pcurItem != nullptr)
        {
            menu.addAction(QIcon(),tr("RemoveData"),this,SLOT(slot_RemoveData()));
            menu.addAction(QIcon(),tr("EditData"),this,SLOT(slot_EditData()));
            menu.addAction(QIcon(),tr("AddChildData"),this,SLOT(slot_AddChildData()));
        }

        menu.exec(event->globalPos());
    }
    QTreeWidget::mousePressEvent(event);
}
